<?php

/**
 * login actions.
 *
 * @package    ecampus
 * @subpackage login
 * @author     Grupo 608
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $q = Doctrine_Query::create()
            ->select('a.id_usuario')
            ->from('usuario a')
            ->where('a.nombre_usuario = ?',$request->getGetParameter('nombre_usuario'))
            ->fetchOne();
//    if($q == 'A')  {$modulo = 'alumno'; }

//    $accion = 'index';
//    $modulo = 'usuario' ;
//    $this->forward($modulo, $accion);
  }
  public function executeLogin(sfWebRequest $request)
  {
      $this->form = new LoginForm();
      $this->executePrepareLogin($request);
      
//      if($request->isMethod('post')){
//          $this->form->bind($request->getParameter('login'));
//          if($this->form->isValid()){
//              //autenticar el usuario y redirigirlo
//              $this->getUser()->setAuthenticated(true);
//              $this->getUser()->addCredential('user');
//              $this->redirect('usuario/index');
//          }
//      }
        //sfView::SUCCESS;
  }
  public function executePrepareLogin(sfWebRequest $request)
  {
      if($this->getRequest()->getMethod() != sfRequest::POST){
          sfView::SUCCESS;
      }
      else
      {
          $usr = $this->getRequestParameter('username');
          $psw = $this->getRequestParameter('password');

//          $psw = md5($psw);

//          $c = new Criteria();
//          $c->add(AuthorPeer::USERNAME,$usr);
//          $c->add(AuthorPeer::PASSWORD,$psw);
          $u = $request->getParameter('nombre_usuario');
          $p = $request->getParameter('password');

          assert(!is_null($usr));
          assert(!is_null($psw));

          $c = Doctrine_Core::getTable('usuario')->retrieveByUsername($usr);
          //$usuario = Doctrine_Core::getTable('Usuario')->r
          //$resultados = AuthorPeer::doSelect($c);

          
          
          //if($resultados){
          if(!is_null($c))
          {
          //si el usuario existe
            if( $c->checkPassword($psw) )
            {
                $this->forward('recursos', 'index');
                $this->setFlash('msg','Login correcto');
                $this->getUser()->setAuthenticated(true);
                $this->getUser()->setAttribute('logged', true);

                $this->redirect('usuario/index');
            }
            else
            {
              $this->setFlash('msg','Login incorrecto');
            }
          }
          else
          {
              $this->forward('materia', 'index');
          }
      }
  }
  public function executeLogout(sfWebRequest $request)
  {
      $this->getUser()->clearCredentials();
      $this->getUser()->setAuthenticated(false);
      $this->redirect('homepage');
  }
}
